package com.dao.impl;

import com.dao.BaseDao;
import com.dao.intf.CollectionDao;
import com.entity.Collection;
import com.entity.Favour;

import java.util.List;


public class CollectionDaoImpl extends BaseDao implements CollectionDao {
    @Override
    public int saveCollection(Integer blogId, Integer userId) {

        String sql="INSERT INTO `collection` (`blogId`,`userId`) VALUES(?,?)";
        return update(sql,blogId,userId);
    }

    @Override
    public int deleteCollection(Integer blogId, Integer userId) {
        String sql="DELETE FROM `collection` WHERE `blogId` =? AND `userId`=?";
        return update(sql,blogId,userId);
    }

    @Override
    public boolean isCollection(Integer blogId, Integer userId) {


        String sql=" SELECT  * FROM `collection` WHERE `blogId`=? AND `userId`=?";
        List<Collection> collections = queryForList(Collection.class, sql, blogId, userId);
        if(collections.size()==0){
            //数据库里没查到，证明当前用户没有收藏当前博客
            return true;

        }else {
            //证明当前用户有收藏当前博客
            return false;
        }

    }

    @Override
    public List<Collection> queryCollectionBlogIdByUserId(Integer userId) {
        String sql="SELECT  `blogId` FROM `collection` WHERE `userId`=?";

        return  queryForList(Collection.class,sql,userId);
    }

    @Override
    public Integer queryBlogCollectionNum(Integer blogId) {

        String sql="SELECT  COUNT(*) FROM `collection` WHERE `blogId`=?";

        Number number = (Number) queryForSingleValue(sql, blogId);

        return number.intValue();
    }
}
